var yhsxdr = {
    setBtnStatus: function () {
        var row = $('#grid_yhsxdr').datagrid('getSelected');
        if (row && row.id) {
            //未导入
            if (row.czzt == 'wdr') {
                $("#btn_yhsxdr_insert").show();//导入
                $("#btn_yhsxdr_cx").hide();//撤销
                $("#btn_yhsxdr_cz").hide();//重置
                $("#btn_yhsxdr_qx").show();//取消
            }
            if (row.czzt == 'drcg') {
                $("#btn_yhsxdr_insert").hide();//导入
                $("#btn_yhsxdr_cx").show();//撤销
                $("#btn_yhsxdr_cz").hide();//重置
                $("#btn_yhsxdr_qx").hide();//取消
            }
            if (row.czzt == 'drsb') {
                $("#btn_yhsxdr_insert").hide();//导入
                $("#btn_yhsxdr_cx").hide();//撤销
                $("#btn_yhsxdr_cz").show();//重置
                $("#btn_yhsxdr_qx").hide();//取消
            }
            if (row.czzt == 'sccg') {
                $("#btn_yhsxdr_insert").show();//导入
                $("#btn_yhsxdr_cx").hide();//撤销
                $("#btn_yhsxdr_cz").hide();//重置
                $("#btn_yhsxdr_qx").show();//取消
            }
            if (row.czzt == 'scsb') {
                $("#btn_yhsxdr_insert").hide();//导入
                $("#btn_yhsxdr_cx").hide();//撤销
                $("#btn_yhsxdr_cz").show();//重置
                $("#btn_yhsxdr_qx").hide();//取消
            }
        } else {
            $("#btn_yhsxdr_insert").hide();//导入
            $("#btn_yhsxdr_cx").hide();//撤销
            $("#btn_yhsxdr_cz").hide();//重置
            $("#btn_yhsxdr_qx").hide();//取消
        }
    }
}


$(function () {
    //表格初始加载
    $('#grid_yhsxdr').datagrid({
        url:$.webPath() + "/cwgl/yhsxdr/yhsxdrList",
        toolbar:"#toolbar_yhsxdr",
        fitColumns:true,
        singleSelect:true,
        pagination:true,
        fit:true,
        pageNumber: 1,
        rownumbers:true,
        striped:true,
        columns:[[
            {field:'yhsx_nbgs_jc', title:'公司名称', width:25,align: 'center'},
            {field:'yhsx_jkrq', title:'借款日期', width:25,align: 'center'},
            {field:'yhsx_khh_qc', title:'还款银行', width:25,align: 'center'},
            {field:'yhsxsflx', title:'银行授信类型', width:25,align: 'center',
                formatter:function(value){
                    if(value =='fk')
                       return "还款";
                    if(value =='sk')
                        return "借款";
                }},
            {field:'yhsx_jkje', title:'借款金额(元)', width:25,align: 'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                }},
            {field:"hkje" ,title:"还款金额(元)",width:20,align: 'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                }},
            {field:"hxje" ,title:"还息金额(元)",width:20,align: 'right',
                formatter:function(value){
                    var sl = formatJe2(value,",");
                    return sl;
                }},
            {field:'hkrq', title:'还款日期', width:25,align: 'center'},
            {field:'zffs_z', title:'支付方式', width:25,align: 'center'},
            {field:'bz', title:'备注', width:25,align: 'center'},
            {field:'voucherNum', title:'凭证号', width:15,align: 'center'},
            {field: 'czzt', title: '状态', width: 10,align: 'center', formatter: function (value, row, index) {
                if (value == 'wdr')
                    return "未导入";
                if (value == 'drcg')
                    return "导入成功";
                if (value == 'drsb')
                    return "导入失败";
                if (value == 'sccg')
                    return "删除成功";
                if (value == 'scsb')
                    return "删除失败";
                return "";
            }}
        ]],
        onLoadSuccess:function(){
            var rows = $('#grid_yhsxdr').datagrid("getRows");
            if(rows.length>0){
                $('#grid_yhsxdr').datagrid("selectRow", 0);
            }
            yhsxdr.setBtnStatus();
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function(){
            yhsxdr.setBtnStatus();
        }
    });
    //**********按钮触发事件**********
    // 导入凭证按钮
    $("#btn_yhsxdr_insert").click(function () {
        yhsxdr_insert();
    });
    //撤销
    $("#btn_yhsxdr_cx").click(function () {
        yhsxdr_cx();
    });
    //重置
    $("#btn_yhsxdr_cz").click(function () {
        yhsxdr_cz();
    });
    //查询按钮
    $("#btn_yhsxdr_search").click(function () {
        yhsxdr_search();
    });
    //取消
    $("#btn_yhsxdr_qx").click(function () {
        yhsxdr_qx();
    });


    //加载查询条件中的申报公司下拉框
    $("#yhsxdr_gs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
});

//删除付款审核
function yhsxdr_cx(){
    var row = $('#grid_yhsxdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否撤销该银行授信？', function (r) {
            if (r) {
                var index = $('#grid_yhsxdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/yhsxdr/revoke_voucher?yhsxdrId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_yhsxdr").mask();
                    },
                    success:function(msg){
                        $("#layout_yhsxdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_yhsxdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }else {
                            $('#grid_yhsxdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要撤销的银行授信！", "warning");
    }
}

//银行还款取消
function yhsxdr_qx(){
    var row = $('#grid_yhsxdr').datagrid('getSelected');
    if(row.yhsxsflx=='sk'){
        $.messager.alert("提示", "只可退回还款类型的银行授信！", "warning");
        return ;
    }
    if (row) {
        $.messager.confirm('提示', '是否取消该还款记录？', function (r) {
            if (r) {
                var index = $('#grid_yhsxdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/yhsxdr/revoke_note?yhsxdrId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_yhsxdr").mask();
                    },
                    success:function(msg){
                        $("#layout_yhsxdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_yhsxdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }else {
                            $('#grid_yhsxdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的票据！", "warning");
    }
}


//删除付款审核
function yhsxdr_cz(){
    var row = $('#grid_yhsxdr').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否重置该银行授信？', function (r) {
            if (r) {
                var index = $('#grid_yhsxdr').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/cwgl/yhsxdr/rest_voucher?yhsxdrId="+row.id,
                    dataType: 'json',
                    type: 'POST',
                    beforeSend: function () {
                        $("#layout_yhsxdr").mask();
                    },
                    success:function(msg){
                        $("#layout_yhsxdr").mask("hide");
                        if (msg.responseflag == 0){
                            $('#grid_yhsxdr').datagrid('reload');
                            $.messager.alert("提示", msg.responsemessage, "error");
                        }else {
                            $('#grid_yhsxdr').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: msg.responsemessage,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要重置的银行授信！", "warning");
    }
}


//弹银行授信贴现窗口
function yhsxdr_insert() {
    var row = $('#grid_yhsxdr').datagrid('getSelected');
    if(row){
        var win = $.window({
            title:"银行授信导入",
            width:1000,
            height:300,
            cache:false,
            url:$.webPath() + "/cwgl/yhsxdr/yhsxdr_insert?yhsxdrId="+row.id,
            modal:true,
            buttons:[
                {
                    text:'确认',
                    handler:function () {
                        $(win).mask();
                        $.ajax({
                            url:$.webPath() + "/cwgl/yhsxdr/generate_voucher",
                            dataType: 'json',
                            type: 'POST',
                            data:{"yhsxdrSystemDate":$("#yhsxdr_systemDate").datebox("getValue"),
                                "attachment":$("#yhsxdr_attachment").val(),
                                "bz":$("#yhsxdr_bz").val(),
                                "yhsxdrId":$("#yhsxdr_id").val()},
                            beforeSend: function () {
                              if($("#yhsxdr_attachment").val()=="" || $("#yhsxdr_attachment").val()==null){
                                  $.messager.alert("提示","请填写附单据数", "error");
                                  $(win).mask('hide');
                                  return false;
                              }
                                if($("#yhsxdr_systemDate").datebox("getValue")=="" || $("#yhsxdr_systemDate").datebox("getValue")==null){
                                    $.messager.alert("提示","请填写制单时间", "error");
                                    $(win).mask('hide');
                                    return false;
                                }
                            },
                            success:function(msg){
                                $(win).mask('hide');
                                $(win).dialog('close');
                                if (msg.responseflag == 0){
                                    $('#grid_yhsxdr').datagrid('reload');
                                    $.messager.alert("提示", msg.responsemessage, "error");
                                }else {
                                    $('#grid_yhsxdr').datagrid('reload');
                                    $.messager.show({
                                        title: '提示',
                                        msg: msg.responsemessage,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                }
                            }
                        });

                    }
                }
            ]
        });
    }else{
        $.messager.alert('提示', '请选择一行数据', 'error');
    }
}

//查询销售单结算
function yhsxdr_search() {
    var gsId = $('#yhsxdr_gs').combotree('getValue') == null ? null : $('#yhsxdr_gs').combotree('getValue');
    var yhsxdrZt = $('#yhsxdr_zt').combobox('getValue') == null ? null : $('#yhsxdr_zt').combobox('getValue');
    $('#grid_yhsxdr').datagrid('load',
        {
            jsdh:$('#yhsxdr_jsdh').val(),
            gsId:gsId,
            yhsxdrZt:yhsxdrZt,
            voucherNum:$("#yhsxdr_pzh").val(),
            czr:$("#yhsxdr_czr").val()
        }
    );
}
